#ifndef PHRASE_H
#define PHRASE_H
#include <QString>
#include <QStringList>
#include <QDomDocument>

/**
  Core functionality. Part of back end. Qt-independent.

  A phrase is the basic object in our SASL tutor program. Using the Phrase
  Creator GUI or by writing it by hand, the user can create simple XML files
  to store the filenames of video, cartoon and icon assosciated with every 
  phrase.
  
  The Phrase class provides methods to access each one of these variables and
  two special methods to save a Phrase object to XML or load it from an XML-Node.

  'lesson' gives a double-link back to the 'parent' Lesson object. Defaults to
  'No category'
  
  Rainer Dreyer

  usage example:
    Phrase ph;
    ph.setName("Testing");
    ph.setCartoonFilename("/to/cartoon.png");
    ph.setIconFilename("/icons/still_imaginary!");
    ph.setDifficulty(2);
    QStringList list("trying out");
    list += "investigating";
    ph.setSynonyms(list);
    ph.setVideoFilename("/videos/test.mpg");

    QDomDocument root;
    QDomElement node = root.createElement("doc");
    ph.saveToChild(node); //<-------------------This is the important function!
    //This function is sneaky enough to use both a document or a non-null node as input
    //a non-null node has to be created off a document!

    QFile f("printedNode.xml");
    f.open(QIODevice::WriteOnly);
    QTextStream s(&f);
    node.save(s,0); //<----------This prints to the QFile

    Phrase p2;
    p2.loadFromNode(node); //<----------------Second magic function.
    //easy to use: you can pass it a document and it'll find the first child phrase
    //or you can pass in the phrase node directly and both will initialize this
    //Phrase object correctly
  */

class Phrase
{
    public:
        // CONSTRUCTORS ///////////////////////////////////////
        /*
         * Default constructor
         */
        Phrase();
        /*
         * Parameterised constructors
         */
        Phrase(const QString& name);
	Phrase(const QString& name, const QString& iconFilename);
        /*
         * Copy constructor. Don't forget to copy fields across in
         * this method!
         */
        Phrase(const Phrase&);

        // XML INTERFACE ///////////////////////////////////////

        /**
         * Get the XML DOM node representation for this Phrase object
         * and add it as a child of the given DOM node.
         * @param QDomNode The desired parent of this object's node representation.
         */
        void saveToChild(QDomNode&) const;
        /**
         * Configure this phrase from an XML DOM node.
         * @param QDomNode The node which will initialise this Phrase object.
         * @return A copy of this phrase object for convenience. The Phrase is
         * also mutated.
         */
        Phrase& loadFromNode(QDomNode);

	// delete the words from memory. this is useful
	// once all phrases and synonyms have been read into
	// the Thesaurus; local synonyms become redundant
	void purgeSynonyms();

        // MUTATORS ///////////////////////////////////////////////
        void setName(QString);
        void setCartoonFilename(QString);
        void setVideoFilename(QString);
        void setIconFilename(QString);
        void setDifficulty(int); //Enforces values 0, 1 & 2 (Easy, intermediate, hard)
        void setSequence(int); //Enforces 0-4 for very early to very late
        void setSynonyms(QStringList);
        void setLesson(QString lessonName);

        // ACCESSORS //////////////////////////////////////////////
        QString getName() const;
        QString getCartoonFilename() const;
        QString getVideoFilename() const;
        int getDifficulty() const;
        int getSequence() const;
        QStringList getSynonyms() const;
        QString getIconFilename() const;
        QString getLesson() const;

        // OVERLOADED OPERATORS ///////////////////////////////////
        /*
         * Overloaded equality operator.
         * Two phrases are defined as equal if they have the same 'name'.
         */
	bool operator== (const Phrase& other) const;
        /*
         * Allows a QString to be compared directly to a Phrase for
         * getName() equality.
         */
        bool operator== (const QString& other) const;

        /**
         * Less-than operator is necessary for using
         * Phrase as the key in a QMap. Performs less-than
         * on getName() QString's.
         * @param Phrase& other The other object against which this is compared.
         */
        bool operator< (const Phrase& other) const;

    private:
        //private members
        QString name;
        QString cartoonFilename;
        QString videoFilename;
        int difficulty;
        int sequence;
        QStringList synonyms;
        QString iconFilename;

        // A double-link back to the lesson who owns this Phrase.
        // This is set externally. Only the master phrase collection
        // has this attribute set.
        QString lesson;
};

#endif // PHRASE_H
